#!/usr/bin/env python
# -*- coding: utf-8 -*-

from rest_framework.pagination import PageNumberPagination

from apps.project.models import Project
from apps.project.serializers import ProjectSerializers, ProjectListSerializers
from utils.rest.views import CustomViewBase


class ProjectPagination(PageNumberPagination):
    """
    测试项目列表自定义分页
    """

    # 默认每页显示的个数
    page_size = 10
    # 可以动态改变每页显示的个数
    page_size_query_param = 'limit'
    # 页码参数
    page_query_param = 'page'
    # 最多能显示多少页
    max_page_size = 100


class ProjectViewSet(CustomViewBase):
    """
    测试项目列表页
    """
    queryset = Project.objects.all()
    # 分页
    pagination_class = ProjectPagination
    # filter_backends = (DjangoFilterBackend,)
    # filter_class = ProjectFilter  , filters.SearchFilter, filters.OrderingFilter
    filter_fields = ('type', )
    search_fields = ('name', 'type')
    ordering = ('add_time',)

    def get_serializer_class(self):
        if self.action == 'list':
            return ProjectListSerializers
        else:
            return ProjectSerializers
